package com.anuo.app.study.studyjava;

import com.anuo.app.common.globalhandle.GlobalConfig;
import com.anuo.app.common.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.xml.bind.SchemaOutputResolver;
import java.nio.charset.Charset;
import java.text.MessageFormat;
import java.util.*;

@RunWith(SpringJUnit4ClassRunner.class)     //表示继承了SpringJUnit4ClassRunner类
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:spring/spring-context.xml"})

/**
 * 字符串
 * 研究状态: 常用的未完成
 * #字符串中带引号
 * #StringJoiner 字符串拼接器(java 8 特性)
 */
public class StudyString {

    /**
     * 字符串常见操作
     */
    @Test
    public void studyString() {
        String name = "my name is jk";
        //字符串格式化
        //String strFormat = String.format("my name is %s and age is %d and address is %s", "jk", 27,"成都");
        //一般用下面这种(注意不能有'这个符号,不然失效,fuck 天坑 https://stackoverflow.com/questions/17569608/format-a-message-using-messageformat-format-in-java)
        //如果非要加这个符号,请双写''
        String strFormat = MessageFormat.format("my name is {0} and age is {1} " +
                "and address is {2}", "jk", 27.66, "成都");
        System.out.println(strFormat);

        //直接写json
        String jsonStr = "{\"name\":\"jk\"}";
        //or
        String jsonStr1 = "{'name':'jk'}";

        //字符串常用API
        name.charAt(1); //返回指导位置出现的字符
        //截取字符串 ,截取的值 开始位置包含, 结束位置不包含 . 如 这段代码 开始位置包含n, 结束位置不包含s
        String result = name.substring(name.indexOf("n"), name.indexOf("s"));
        //字符串转数组
        String ids = "jk,anuo,fuck";
        String ids2 = "jk|anuo|fuck";
        String[] arrayIds = ids.split(",");
        String[] arrayIds2 = ids2.split("\\|");
        //数组转字符串
        String strNew = StringUtil.join(arrayIds, ",");// Apache Commons Lang
        String str = String.join(",", arrayIds2);//java 8

        int[] intArray = {1, 2, 3, 4};
        String str2 = Arrays.toString(intArray);

        //转义字符
        //https://stackoverflow.com/questions/1367322/what-are-all-the-escape-characters-in-java
        // \"" 双引号
        // \'' 单引号
        // \\  反斜杠

        //替换字符串
        String strReplace = "替换字符串";
        strReplace = strReplace.replace("字符串", "方向");
        //去掉空格
        strReplace = strReplace.trim();
        //剪掉开始或尾部指定字符
        str = "cpu";
        str = StringUtil.removeEnd(str, "pu");//str 的值为 "c"
        str = StringUtil.removeStart(str, "cp");//str 的值为 "u"
        //包含判断
        if (str.contains("c")) {
            System.out.println("have c");
        }
        //大小写转换
        str = "Anuo";
        str = str.toUpperCase();
        System.out.println(str);
        str = str.toLowerCase();
        System.out.println(str);
        //不相等判断
        if (!"jk".equals("anuo")) {
            System.out.println("not equal");
        }
        //object to string 用toString
        Map<String, Object> map = new HashMap<>();
        map.put("name", "jk");
        String nameValue = map.get("name").toString();
        System.out.println(nameValue);
    }



    /**
     * StringJoiner 拼接有规则的字符串
     */
    @Test
    public void studyStringJoiner() {
        // passing comma(,) and square-brackets as delimiter
        StringJoiner joinNames = new StringJoiner(",", "[", "]");

        // Adding values to StringJoiner
        joinNames.add("Rahul");
        joinNames.add("Raju");
        joinNames.add("Peter");
        joinNames.add("Raheem");

        System.out.println(joinNames);
        //Output: [Rahul,Raju,Peter,Raheem]
    }



    @Test
    public void studyStringBuilder() {
        //字符串拼接 (需要线程安全时用 StringBuffer, 两者API 大体一样)
        //StringBuilder 文档: https://www.javatpoint.com/StringBuilder-class
        StringBuilder sb = new StringBuilder();
        //append
        sb.append("hell");
        sb.append("world");
        StringBuffer sbuf = new StringBuffer();
        sbuf.append("my");
        sbuf.append("nam");
        //delete
        sb.deleteCharAt(1);
        //all string length
        sb.length();
        //转换成string
        System.out.println(sb.toString());
    }

    @Test
    public void studyStringConvert() {
        //string 和 byte[] 互转
        String msg = "hello world";
        byte[] msgByte = msg.getBytes(GlobalConfig.UTF_8);//最好给个编码,不然转换的时候可能会出现编码不一致的问题
        String msgNew = new String(msgByte, GlobalConfig.UTF_8);
        System.out.println(msgNew);
    }


}



